(() => {
  let urlSearchParams = new URLSearchParams(window.location.search);
  const params = Object.fromEntries(urlSearchParams.entries());
  let id = params.id || 0;
  let ele = {};
  ele.list_bt = $("#list_bt");
  ele.jiaCen = $("#jiaCen");
  ele.right_jia = $("#right_jia");
  ele.btn_que3 = $("#btn_que3");
  ele.chahao2 = $("#chahao2");
  ele.paihang = $("#paihang");
  ele.modal_mark = $("#modal-mark");
  ele.left_jian = $("#left_jian");
  ele.more = $("#more");
  ele.topBox = $("#topBox");
  ele.empty = $("#empty");
  ele.dangqian = $("#dangqian");
  let list = [];
  let num = 100;
  let page = 1;
  let obj = {};
  // todo赋值加价格
  ele.jiaCen.value = num;
  // todo点击加的话加100
  ele.right_jia.onclick = () => {
    num = num + 100;
    ele.jiaCen.value = num;
    ele.dangqian.innerHTML = `当前总出价: <span >${
      +obj.cha_money + num
    }金币</span>`;
  };
  // todo点击减的话减100
  ele.left_jian.onclick = () => {
    if (num <= 100) {
      return;
    }
    num = num - 100;
    ele.jiaCen.value = num;
    ele.dangqian.innerHTML = `当前总出价: <span >${
      +obj.cha_money + num
    }金币</span>`;
  };
  // todo点击确认后
  ele.btn_que3.onclick = () => {
    console.log(num);
  };
  // todo点击❌关闭弹窗
  ele.chahao2.onclick = () => {
    num = 100;
    ele.jiaCen.value = num;
    ele.dangqian.innerHTML = `当前总出价: <span >${
      +obj.cha_money + num
    }金币</span>`;
    closeMdl(ele.modal_mark);
  };
  ele.btn_que3.onclick = () => {
    chujia(num);
  };
  // !发送请求
  asyncList();
  function asyncList() {
    let token = getCookie("token");
    ajax({
      method: "get",
      url: API_PATH.JINGPAI,
      header: {
        Authorization: token,
      },
      data: {
        page: page,
        id: id,
        // size: 1,
      },
      success: (res) => {
        if (res.code == 1) {
          obj = res.data.detail;
          // list = res.data.list.data;
          list = [...list, ...res.data.list.data];
          if (list.length === 0) {
            page = 0;
            ele.more.style.display = "none";
            ele.empty.style.display = "block";
          } else {
            initList();
            page++;
            if (page > res.data.list.last_page) {
              ele.more.innerText = "没有更多了";
              ele.more.onclick = null;
            } else {
              ele.more.innerText = "加载更多";
              ele.more.onclick = () => asyncList(page);
            }
          }
        } else openTips(res.msg);
        // obj = res.data.detail;
        // list = [...list, ...res.data.list.data];
        initList();
      },
      error: (err) => {
        // openTips(err.msg);
      },
    });
  }

  // !渲染数据
  function initList() {
    // console.log(list, "0101");
    ele.dangqian.innerHTML = `当前总出价: <span >${
      +obj.cha_money + num
    }金币</span>`;
    let newList = list
      .map((item) => {
        return `
  <div class="list_son">
  <div class="left">
    <div class="img">
      <img src="${
        item.agent_info && item.agent_info.head_pic
          ? IMG_DOMAIN + item.agent_info.head_pic
          : item.agent_info.sex == 1
          ? "http://img.qxgs.cc/tiantianlaishouzu/img/my/tx1.png"
          : "http://img.qxgs.cc/tiantianlaishouzu/img/my/tx2.png"
      }" alt="" />
    </div>
    <div class="dongname">
      <p class="t">${
        item.agent_info && item.agent_info.nick_name
          ? item.agent_info.nick_name
          : "杜拉拉"
      }</p>
      <p>${item.created_at}</p>
    </div>
  </div>
  <div class="right">加价:${item.add_money}<br>当前出价:${item.money}</div>
</div>
  `;
      })
      .join(" ");
    ele.list_bt.innerHTML = newList;

    //!渲染上面内容
    ele.topBox.innerHTML = `
<div class="top_son">
当前拍卖物品 <span id="guan">（当前为官方拍卖）</span>
</div>
<!-- 下面的房子 -->
<div class="home_son">
<p>${obj.house_detail.name}</p>
<img src="${IMG_DOMAIN + obj.house_detail.logo}" alt="" />
<p>房产编号：${obj.no}</p>
</div>
<div class="home_name">基础价格:<img src="http://img.qxgs.cc/tiantianlaishouzu/img/public/jinbi.png" alt="" /><span>${
      obj.money
    }</span></div>
<div class="home_name">
${
  obj.agent_info && obj.agent_info.nick_name
    ? obj.agent_info.nick_name
    : "杜拉拉"
} &nbsp;&nbsp;&nbsp; 最高为
<img src="http://img.qxgs.cc/tiantianlaishouzu/img/public/jinbi.png" alt="" /> ${obj.over_money}
</div>
<div class="btn_que" id="btn_que2">加价竞拍</div>
<div class="shengTimg">
本次拍卖剩余时间：<span data-time="${
      obj.over_time
    }" id="day">0小时00分00秒</span>
</div>`;

    // todo是否显示当前为官方竞拍
    ele.guan = $("#guan");
    if (id != 1) {
      closeMdl(ele.guan);
    }
    // 倒计时
    // !剩余时间
    let dayAll = $("#day");

    let time2 = null;
    time2 = setInterval(() => {
      if (dayAll.dataset.time * 1000 <= +new Date()) {
        dayAll.innerHTML = `已结束`;
        console.log(55555);
        clearInterval(time2);
      } else {
        dayAll.innerHTML = `${getCountTimeDay(dayAll.dataset.time)}`;
      }
    }, 1000);

    //加价竞拍
    ele.btn_que2 = $("#btn_que2");
    // todo点击加价竞拍弹出框
    ele.btn_que2.onclick = () => {
      ele.chujia = $("#chujia");
      ele.chujia.innerHTML = `当前出价<span class="zzdzj" id="zzdzj">${obj.over_money}金币</span>购买`;
      ele.yongyou = $("#yongyou");
      ele.yongyou.innerHTML = obj.gold;
      openMdl(ele.modal_mark);
    };
    // !是否显示竞拍按钮
    if (obj.is_my == 1) {
      closeMdl(ele.btn_que2);
    } else {
      openMdl(ele.btn_que2);
    }

    // todo获取上面盒子高度
    let topHeiiht = ele.topBox.offsetHeight + 100;
    // console.log(topHeiiht, "0000");
    ele.paihang.style.height = `calc(100vh - ${topHeiiht}px)`;
  }

  function chujia(num) {
    let token = getCookie("token");
    ajax({
      method: "post",
      url: API_PATH.CHUJIA,
      header: {
        Authorization: token,
      },
      data: {
        num: num,
        id: id,
      },
      success: (res) => {
        openTips(res.msg);
        asyncList();
        closeMdl(ele.modal_mark);
        // page = 1;
      },
    });
  }
})();
